var Calendar = new Class({
    options: {
        beginYear: new Date().getFullYear(),
        beginMonth: new Date().getMonth() + 1,
        beginDate: new Date().getDate(),
        beginDay: new Date().getDay() - 1,
        target: null,
        nextfocus: false,
        templateobj: null,
        selectday: function() { },
        className: 'Calendar',
        template: '<div class="title"><a href="javascript:void(0);" class="prev">&nbsp;&nbsp;</a><span></span><a class="next" href="javascript:void(0);">&nbsp;&nbsp;</a></div><div><ul class="day"><li style="color:Red;font-weight:bold">日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li style="color:yellow;font-weight:bold">六</li></ul></div><div><ul class="date"><li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li><li></li> <li></li> <li></li> <li></li> <li></li> <li></li></ul></div>'
    },
    initialize: function(option, obj) {
        this.setOptions(option);
        this.beginDay = this._getBeginDay(this.beginYear, this.beginMonth - 1, 1);
    },
    _getBeginDay: function(y, m, d) {
        return new Date(y, m, d).getDay();
    },
    calc: function() {
        var This = this;
        var data = new Date(this.beginYear, this.beginMonth, 0);
        var day = data.getDate();
        this.templateobj.getElementsByTagName('div')[0].getElementsByTagName('span')[0].innerHTML = this.beginYear + "年" + (this.beginMonth) + '月';
        this.templateobj.getElementsByTagName('div')[0].getElementsByTagName('a')[0].onclick = function() { This.prev() };
        this.templateobj.getElementsByTagName('div')[0].getElementsByTagName('a')[1].onclick = function() { This.next() };
        this.clear();
        var startObj = this.templateobj.getElementsByTagName('div').item(2).getElementsByTagName('li').item(this.beginDay);
        for (var i = 0; i < day; i++) {
            if (i == this.options.beginDate) {
                startObj.previousSibling.previousSibling.className = 'datehove';
            }
            startObj.innerHTML = i + 1;
            startObj.onmouseover = function() {
                if (this.innerHTML.length > 0) {
                    this.className = "datehove";
                    this.style.color = "#fff";
                }
            }
            startObj.onmouseout = function() {

                this.className = "";
                this.style.color = "#000";

            }
            startObj.onclick = (function(year, month, day, next) {
              
                return function(e) {
                    if (e) e.cancelBubble = true;
                    This.selectday(year, month, day);
                    if (This.target.tagName == "INPUT") {
                        This.target.value = year + '/' + month + '/' + day;
                        This.templateobj.style.display = "none";
                        if (next) {
                            var obj = This.target.nextSibling;
                            while (obj.nodeType != 1) {
                                obj = obj.nextSibling;
                            }
                            obj.focus();
                        }
                    }
                }
            })(This.beginYear, This.beginMonth, i + 1, This.nextfocus);
            startObj = startObj.nextSibling.nodeType == 3 ? startObj.nextSibling.nextSibling : startObj.nextSibling;
        }
    },
    _bind: function() {
        var This = this;
        if (this.target.tagName == "DIV") {
            This.templateobj.style.display = "block";
        } else {
            this.target.onfocus = function() {
                for (var i = 0; i < document.getElementsByTagName('div').length; i++) {
                    if (document.getElementsByTagName('div')[i].className == This.className && document.getElementsByTagName('div')[i].parentNode.tagName == "BODY") {
                        document.getElementsByTagName('div')[i].style.display = 'none';
                    }
                }
                This.templateobj.style.display = "block";
                This.templateobj.style.top = this.offsetTop + this.offsetHeight + 'px';
                This.templateobj.style.left = this.offsetLeft + 'px';
            }
        }
    },
    run: function() {
        var This = this;
        if (typeof (this.target) == 'string') {
            var Calendar_div = document.createElement('div');
            Calendar_div.className = This.className;
            Calendar_div.id = This.className + '_' + this.target;
            Calendar_div.innerHTML = this.options.template;

            if (document.getElementById(this.target).tagName != "INPUT") {
                document.getElementById(this.target).appendChild(Calendar_div);
            } else {
                document.body.appendChild(Calendar_div);
            }
            this.templateobj = Calendar_div;
            this.target = document.getElementById(this.target);
            this.target.setAttribute('mode', 'Calendar');
        }
        if (this.target.tagName == "INPUT") {
            document.documentElement.onclick = function(e) {
                var tag = e ? e.target : window.event.srcElement;
                if (tag.getAttribute('mode') != 'Calendar') {
                    if (tag.className == 'next' || tag.className == 'prev') {
                        return false;
                    } else {
                        for (var i = 0; i < document.getElementsByTagName('div').length; i++) {
                            if (document.getElementsByTagName('div')[i].className == This.className && document.getElementsByTagName('div')[i].parentNode.tagName == "BODY") {
                                document.getElementsByTagName('div')[i].style.display = 'none';
                            }
                        }
                    }
                }
            }
        }
        this._bind();
        this.calc();
    },
    clear: function() {
        var This = this;
        var obj = This.templateobj.getElementsByTagName('div').item(2).getElementsByTagName('li');
        for (var i = 0; i < obj.length; i++) {
            This.templateobj.getElementsByTagName('div').item(2).getElementsByTagName('li')[i].className = "";
            This.templateobj.getElementsByTagName('div').item(2).getElementsByTagName('li')[i].innerHTML = "";
        }
    },
    prev: function() {
        if (this.beginMonth == 1) {
            this.beginMonth = 12;
            --this.beginYear;
            this.beginDay = new Date(this.beginYear, 11, 1).getDay();
        } else {
            var t = --this.beginMonth;
            this.beginDay = new Date(this.beginYear, --t, 1).getDay();
        }
        this.run();
    },
    next: function() {
        if (this.beginMonth == 12) {
            this.beginMonth = 1;
            ++this.beginYear;
            this.beginDay = new Date(this.beginYear, 0, 1).getDay();
        } else {
            var t = ++this.beginMonth;
            this.beginDay = new Date(this.beginYear, --t, 1).getDay();
        }
        this.run();
    }
});